package com.fmdj.bff.driver.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.json.JSONUtil;
import com.fmdj.bff.driver.controller.form.*;
import com.fmdj.bff.driver.service.DriverService;
import com.fmdj.common.util.CommonResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/driver")
@Tag(name = "DriverController", description = "司机端模块接口")
public class DriverController {
    @Resource
    private DriverService driverService;

    @PostMapping("/registerNewDriver")
    @Operation(summary = "司机注册接口")
    public CommonResult registerDriver(@Valid @RequestBody RegisterDriverForm form) {
        long driverId = driverService.registerDriver(form);
        StpUtil.login(driverId);
        String token = StpUtil.getTokenInfo().getTokenValue();
        return CommonResult.ok().put(CommonResult.RETURN_TOKEN, token);
    }

    @PostMapping("/updateDriverAuth")
    @Operation(summary = "司机实名认证接口")
    @SaCheckLogin
    public CommonResult updateDriverAuth(@Valid @RequestBody UpdateDriverAuthForm form) {
        long driverId = StpUtil.getLoginIdAsLong();
        form.setDriverId(driverId);
        int result = driverService.updateDriverAuth(form);
        return CommonResult.ok().put(CommonResult.RETURN_ROW, result);
    }

    @PostMapping("/login")
    @Operation(summary = "司机登录接口")
    public CommonResult login(@Valid @RequestBody LoginForm form) {
        HashMap loginResult = driverService.login(form);
        if (loginResult != null) {
            long driverId = MapUtil.getLong(loginResult, "id");
            byte realAuth = Byte.parseByte(MapUtil.getStr(loginResult, "realAuth"));
            boolean archive = MapUtil.getBool(loginResult, "archive");
            StpUtil.login(driverId);
            String token = StpUtil.getTokenInfo().getTokenValue();
            return CommonResult.ok().put(CommonResult.RETURN_TOKEN, token).put("realAuth", realAuth).put("archive", archive);

        }
        return CommonResult.ok();
    }

    @GetMapping("/logout")
    @Operation(summary = "司机退出系统接口")
    @SaCheckLogin
    public CommonResult logout() {
        StpUtil.logout();
        return CommonResult.ok();
    }

    @PostMapping("/selectDriverInfo")
    @Operation(summary = "查询司机个人信息接口")
    @SaCheckLogin
    public CommonResult selectDriverInfo() {
        long driverId = StpUtil.getLoginIdAsLong();
        SelectDriverInfoForm selectDriverInfoForm = new SelectDriverInfoForm();
        selectDriverInfoForm.setDriverId(driverId);
        HashMap result = driverService.selectDriverInfo(selectDriverInfoForm);
        return CommonResult.ok().put(CommonResult.RETURN_RESULT, result);
    }

    @PostMapping("/selectWorkbenchData")
    @Operation(summary = "查询司机工作台数据")
    @SaCheckLogin
    public CommonResult selectWorkbenchData() {
        long driverId = StpUtil.getLoginIdAsLong();
        HashMap result = driverService.selectWorkbenchData(driverId);
        return CommonResult.ok().put(CommonResult.RETURN_RESULT,result);
    }

    /*
     * 查询司机实名认证信息接口
     * */
    @GetMapping("/searchDriverAuth")
    @Operation(summary = "查询司机实名认证信息接口")
    @SaCheckLogin
    public CommonResult selectDriverInfoById(){
        long driverId = StpUtil.getLoginIdAsLong();
        SelectDriverAuthForm form = new SelectDriverAuthForm();
        form.setDriverId(driverId);
        HashMap map = driverService.selectDriverInfoById(form);
        return CommonResult.ok().put(CommonResult.RETURN_RESULT,map);
    }

    @PostMapping("/updateDriverRealAuth")
    @SaCheckPermission(value = {"ROOT", "DRIVER:UPDATE"}, mode = SaMode.OR)
    @Operation(summary = "更新司机实名认证状态")
    public CommonResult updateDriverRealAuth(@RequestBody @Valid UpdateDriverRealAuthForm form){
        int rows = driverService.updateDriverRealAuth(form);
        return CommonResult.ok().put(CommonResult.RETURN_ROW,rows);
    }


}
